//
//  WHYWelcomeViewController.swift
//  WHYWeibo-1.0
//
//  Created by 王洪运 on 15/11/25.
//  Copyright © 2015年 王洪运. All rights reserved.
//

import UIKit

class WHYWelcomeViewController: UIViewController {
    
    override func loadView() {
        view = backImageView
        
        setupUI()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        iconView.sd_setImageWithURL(WHYUserAccountViewModel.sharedUserAccount.avatarURL, placeholderImage: UIImage(named: "avatar_default_big"))
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view).offset(-view.bounds.height + 200)
        }
        
        UIView.animateWithDuration(1.5,
            delay: 0,
            usingSpringWithDamping: 0.8,
            initialSpringVelocity: 8,
            options: [],
            animations: { () -> Void in
//                强制刷新
                self.view.layoutIfNeeded()
//                下次刷新
//                self.view.setNeedsLayout()
//                self.view.setNeedsDisplay()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.8, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1.0
                    }) { (_) -> Void in
                        
                        delay(0.8, callFunc: { () -> () in
                            NSNotificationCenter.defaultCenter().postNotificationName(WHYWBSwitchViewControllerNotification, object: nil)
                        })
                        
                }
        }
    }
    
    private lazy var backImageView: UIImageView = UIImageView(imageName: "ad_background")

    private lazy var iconView: UIImageView = {
        let imgView = UIImageView(imageName: "avatar_default_big")
        imgView.layer.cornerRadius = 45
        imgView.layer.masksToBounds = true
        return imgView
    }()
    
    private lazy var welcomeLabel: UILabel = UILabel(title: "欢迎回来", fontSize: 17)


}

extension WHYWelcomeViewController {
    
    private func setupUI() {
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        welcomeLabel.alpha = 0
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-200)
            make.size.equalTo(CGSizeMake(90, 90))
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
    
}
